//
//  YRJShowMySavedViewController.m
//  YanRenjie
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "YRJShowMySavedViewController.h"
#import "YRJTripHeader.h"

@interface YRJShowMySavedViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@end

@implementation YRJShowMySavedViewController
- (void)dealloc {
    [_tableView release];
    [_savedCountryArray release];
    [_savedCityArray release];
    
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的收藏";
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [_tableView release];
    
    self.tableView.rowHeight = (100.0 / 667) * HEIGHT;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    NSLog(@"%ld", self.savedCityArray.count);
    NSLog(@"%ld", self.savedCountryArray.count);
    
    // 给导航控制器右侧添加一个编辑按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    // 开启编辑模式
//    [self.tableView setEditing:YES animated:YES];
}

#pragma mark - tableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.savedCountryArray.count;
    } else {
        return self.savedCityArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *countryReuse = @"countryReuse";
        YRJMySavedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:countryReuse];
        if (!cell) {
            cell = [[YRJMySavedTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:countryReuse];
        }
        if (self.savedCountryArray.count != 0) {
            YRJHotCountryModel *model = self.savedCountryArray[indexPath.row];
            [cell.savedImageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
            cell.savedLabelTwo.text = model.label;
            cell.savedLabelOne.text = model.cnname;
        }
        return cell;
    } else {
        static NSString *cityReuse = @"cityReuse";
        YRJMySavedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cityReuse];
        if (!cell) {
            cell = [[YRJMySavedTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cityReuse];
        }
        if (self.savedCityArray.count != 0) {
            YRJAllHotCityModel *model = self.savedCityArray[indexPath.row];
            [cell.savedImageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
            cell.savedLabelTwo.text = model.representative;
            cell.savedLabelOne.text = model.catename;
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        YRJCountryDetailViewController *countryDetailVC = [[YRJCountryDetailViewController alloc] init];
        [self.navigationController pushViewController:countryDetailVC animated:YES];
        YRJHotCountryModel *model = self.savedCountryArray[indexPath.row];
        countryDetailVC.string = (NSString *)model.id;
        countryDetailVC.countryName = model.cnname;
        countryDetailVC.hotCountrymodel = self.savedCountryArray[indexPath.row];
        [countryDetailVC release];
    } else {
        YRJCityDetailViewController *cityDetailVC = [[YRJCityDetailViewController alloc] init];
        [self.navigationController pushViewController:cityDetailVC animated:YES];
        YRJAllHotCityModel *model = self.savedCityArray[indexPath.row];
        cityDetailVC.string = (NSString *)model.id;
        cityDetailVC.cityName = model.catename;
        cityDetailVC.cityModel = self.savedCityArray[indexPath.row];
        [cityDetailVC release];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"热门国家";
    } else {
        return @"热门城市";
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

#pragma mark - tableView cell的删除功能的相关方法
// 重写系统的编辑按钮的触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:(editing) animated:(animated)];
}


// 修改cell的编辑模式, 默认是删除, 还可以进行添加等操作
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}


// 实现删除
// 还自带了一个左滑显示删除功能的效果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if (indexPath.section == 0) {
            // 找到沙河路径
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            // 拼接路径
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"hot.plist"];
            // 反归档
            self.savedCountryArray = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            // 删除一定要删除对应数据源的数据
            [self.savedCountryArray removeObjectAtIndex:indexPath.row];
            // 单行刷新
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
            // 归档
            [NSKeyedArchiver archiveRootObject:self.savedCountryArray toFile:docPath];
            
        } else {
            // 找到沙河路径
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            // 拼接路径
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"hotCity.plist"];
            // 反归档
            self.savedCityArray = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            // 删除一定要删除对应数据源的数据
            [self.savedCityArray removeObjectAtIndex:indexPath.row];
            // 归档
            [NSKeyedArchiver archiveRootObject:self.savedCityArray toFile:docPath];
            // 单行刷新
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld", tableView.numberOfSections);
    return @"DELETE";
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [self.arr replaceObjectAtIndex:indexPath.row withObject:@"刘超"];
//    // 单行刷新
//    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
